﻿
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Imports DevComponents.DotNetBar
Imports quanlydulich.Helpers

Partial Public Class NHANVIEN
    Inherits Office2007Form
    Private objDS As DataSet
    Friend WithEvents frmChucVu As CHUCVU
    Friend WithEvents frmLoaiNguoiDung As LOAINGUOIDUNG
    ' Hidden files are used to store ids 
    Private txtMaChucVu As DevComponents.DotNetBar.Controls.TextBoxX
    Private txtMaLoaiNguoiDung As DevComponents.DotNetBar.Controls.TextBoxX
    ' Event handler is used to transfer data to receiver
    Public Event Handler(ByVal sender As Object, ByVal e As MyEvent)

    Public Criterial_ID As String
    Public Criterial_Name As String
    Public ModeSearch As Boolean

    Public Sub InitHiddenFields()
        ' Add a new textbox
        txtMaChucVu = New DevComponents.DotNetBar.Controls.TextBoxX() With { _
         .Name = "txtMaChucVu", _
         .Text = "" _
        }
        txtMaChucVu.Visible = False

        txtMaLoaiNguoiDung = New DevComponents.DotNetBar.Controls.TextBoxX() With { _
         .Name = "txtMaLoaiNguoiDung", _
         .Text = "" _
        }
        txtMaLoaiNguoiDung.Visible = False

    End Sub

    Private Sub btnChon_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChon.Click
        Close()
        RaiseEvent Handler(Me, New MyEvent(txtMaNHANVIEN.Text & "#" & txtTenNHANVIEN.Text))
    End Sub

    Private Sub btnNhapMoi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNhapMoi.Click
        EraseTextboxes()
    End Sub

    Public Sub EraseTextboxes()
        txtMaNHANVIEN.Text = ""
        txtTenNHANVIEN.Text = ""
        txtDiaChi.Text = ""
        txtDienThoai.Text = ""
        cbxGioiTinh.SelectedIndex = 0
        txtMaChucVu.Text = ""
        txtChucVu.Text = ""
        txtMaLoaiNguoiDung.Text = ""
        txtLoaiNguoiDung.Text = ""
        txtTenDangNhap.Text = ""
        txtMatKhau.Text = ""
        dtgvDataList.ClearSelection()
    End Sub

    Private Sub btnXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoa.Click
        Dim objBUS As NHANVIENBUS = New NHANVIENBUS(GlobalVars.g_strConnectionString)
        ' Chi thuc hien xoa, khi user select in grid view
        If txtMaNHANVIEN.Text <> "" Then
            If objBUS.Delete(Integer.Parse(txtMaNHANVIEN.Text)) Then
                LoadData()
                EraseTextboxes()
                MessageBox.Show("A new item has been deleted")
            Else
                MessageBox.Show("Delete failed!")

            End If
        End If
    End Sub

    Private Sub btnLuu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLuu.Click
        ' Neu user chua select in grid view -> thuc hien thao tac insert

        Dim errorText As String
        errorText = ""
        If Not ValidateInput(errorText) Then
            MessageBox.Show(errorText)
            Return
        End If

        If txtMaNHANVIEN.Text = "" Then
            InsertNhanVien()
        Else ' Thuc hien thao tac update
            UpdateNhanVien()
        End If
    End Sub

    Public Sub UpdateForeignKeys(ByRef item As NHANVIENDTO)
        If txtMaChucVu.Text <> "" Then
            item.IDCV = Integer.Parse(txtMaChucVu.Text)
        End If
        If txtMaLoaiNguoiDung.Text <> "" Then
            item.IDLoaiNguoiDung = Integer.Parse(txtMaLoaiNguoiDung.Text)
        End If
    End Sub

    Public Sub InsertNhanVien()
        Dim objBUS As NHANVIENBUS = New NHANVIENBUS(GlobalVars.g_strConnectionString)
        Dim objDTO As NHANVIENDTO = New NHANVIENDTO
        objDTO.TenNV = txtTenNHANVIEN.Text
        objDTO.DiaChi = txtDiaChi.Text
        objDTO.NgaySinh = dtNgaySinh.Value
        objDTO.GioiTinh = IIf(cbxGioiTinh.SelectedIndex = 0, True, False)
        objDTO.SoDT = txtDienThoai.Text
        objDTO.TenDangNhap = txtTenDangNhap.Text
        objDTO.MatKhau = Encryption.Encrypt(txtMatKhau.Text)

        UpdateForeignKeys(objDTO)

        If objBUS.Insert(objDTO) Then
            LoadData()
            EraseTextboxes()
            MessageBox.Show("An item has been Added")
        Else
            MessageBox.Show("Add failed")
        End If
    End Sub
    Public Sub UpdateNhanVien()
        Dim objBUS As NHANVIENBUS = New NHANVIENBUS(GlobalVars.g_strConnectionString)
        Dim objDto As NHANVIENDTO = New NHANVIENDTO
        objDto.ID = Integer.Parse(txtMaNHANVIEN.Text)
        objDto.TenNV = txtTenNHANVIEN.Text
        objDto.DiaChi = txtDiaChi.Text
        objDto.NgaySinh = dtNgaySinh.Value
        objDto.GioiTinh = IIf(cbxGioiTinh.SelectedIndex = 0, True, False)
        objDto.SoDT = txtDienThoai.Text
        objDto.TenDangNhap = txtTenDangNhap.Text

        UpdateForeignKeys(objDto)

        If objBUS.Update(objDto) Then
            LoadData()
            MessageBox.Show("A new item has been updated")
        Else
            MessageBox.Show("Update failed!")
        End If
    End Sub

    Public Function ValidateInput(ByRef errorText As String) As Boolean
        If txtTenNHANVIEN.Text = "" Then
            errorText = "Vui lòng nhập tên nhân viên"
            Return False
        End If
        If txtTenDangNhap.Text = "" Then
            errorText = "Vui lòng nhập tên đăng nhập"
            Return False
        End If
        If txtMatKhau.Text = "" Then
            errorText = "Vui lòng nhập mật khẩu"
            Return False
        End If
        Return True
    End Function

    Private Sub btnChonChucVu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChonChucVu.Click
        frmChucVu = New CHUCVU
        frmChucVu.ShowDialog()
    End Sub

    Private Sub frmChucVu_Handler(ByVal sender As Object, ByVal e As MyEvent) Handles frmChucVu.Handler
        Dim eventType = DirectCast(e, MyEvent)
        Dim comp As String() = eventType.Data.Split(New Char() {"#"})

        txtMaChucVu.Text = comp(0) 'id
        txtChucVu.Text = comp(1) 'name
    End Sub

    Private Sub btnChonLoaiNguoiDung_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChonLoaiNguoiDung.Click
        frmLoaiNguoiDung = New LOAINGUOIDUNG
        frmLoaiNguoiDung.ShowDialog()
    End Sub

    Private Sub frmLoaiNguoiDung_Handler(ByVal sender As Object, ByVal e As MyEvent) Handles frmLoaiNguoiDung.Handler
        Dim eventType = DirectCast(e, MyEvent)
        Dim comp As String() = eventType.Data.Split(New Char() {"#"})

        txtMaLoaiNguoiDung.Text = comp(0) 'id
        txtLoaiNguoiDung.Text = comp(1) 'name
    End Sub

    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        Close()
    End Sub

    Private Sub dtgvDataList_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dtgvDataList.CellClick
        Dim selectedRow As Integer = dtgvDataList.CurrentRow.Index
        If selectedRow <> -1 Then
            txtMaNHANVIEN.Text = dtgvDataList.Item(0, selectedRow).Value

            Dim objNhanVienBUS As NHANVIENBUS = New NHANVIENBUS(GlobalVars.g_strConnectionString)
            Dim lstItem As DataTable = objNhanVienBUS.SelectByID(Integer.Parse(txtMaNHANVIEN.Text)).Tables(0)
            If (lstItem.Rows.Count > 0) Then
                txtTenNHANVIEN.Text = lstItem.Rows(0)("TenNV").ToString()
                txtDiaChi.Text = lstItem.Rows(0)("DiaChi").ToString()
                txtDienThoai.Text = lstItem.Rows(0)("SoDT").ToString()
                dtNgaySinh.Value = DateTime.Parse(lstItem.Rows(0)("NgaySinh").ToString())
                cbxGioiTinh.SelectedIndex = IIf(Boolean.Parse(lstItem.Rows(0)("GioiTinh").ToString()) = True, 0, 1)
                txtChucVu.Text = lstItem.Rows(0)("TenCV").ToString()
                txtMaChucVu.Text = lstItem.Rows(0)("IDCV").ToString()
                txtLoaiNguoiDung.Text = lstItem.Rows(0)("TenLoaiNguoiDung").ToString()
                txtMaLoaiNguoiDung.Text = lstItem.Rows(0)("IDLoaiNguoiDung").ToString()
                txtTenDangNhap.Text = lstItem.Rows(0)("TenDangNhap").ToString()
                txtMatKhau.Text = lstItem.Rows(0)("MatKhau").ToString()
            End If
        End If
    End Sub

    Private Sub NHANVIEN_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        InitHiddenFields()
        LoadData()
        CreateGioiTinh()
    End Sub

    ' Load list of NHANVIEN from DB
    Public Sub LoadData()
        Dim objBUS As NHANVIENBUS = New NHANVIENBUS(GlobalVars.g_strConnectionString)

        If Not ModeSearch Then
            objDS = objBUS.SelectAll()
        Else
            objDS = objBUS.Search(Criterial_ID, Criterial_Name)
        End If

        ' Neu ton tai du lieu ung voi NHANVIEN table trong DB
        dtgvDataList.AutoGenerateColumns = False
        dtgvDataList.DataSource = objDS.Tables(0)
        dtgvDataList.ClearSelection()
    End Sub

    Public Sub CreateGioiTinh()
        cbxGioiTinh.Items.Clear()

        Dim gioitinh As GioiTinh = New GioiTinh
        gioitinh.Value = 1
        gioitinh.Name = "Nam"
        cbxGioiTinh.Items.Add(gioitinh)

        gioitinh = New GioiTinh
        gioitinh.Value = 0
        gioitinh.Name = "Nữ"
        cbxGioiTinh.Items.Add(gioitinh)
    End Sub
End Class